using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class admin_PersonelList : PagedControl
{
    string requestString;
    PersonDAO daoPerson = new PersonDAO();
    IList<Person> personList = new List<Person>();
    public override string RequestString
    {
        get { return requestString; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        p = Request["page"];
        requestString = "Default.aspx?ctrl=person_list&page=";
        totals = 0;
        currentPage = GetPageNumber(p);
        ActiveAccording = "person";
        string name = txtSearch.Text;       
        {
        LoadData(pageSize, currentPage, ref totals, name);
        lblPaging.Text = GetPageGrid(totals, pageSize, currentPage);
        }
        
    }




    public void LoadData(int pageSize, int currentPage, ref int totalRecords, string name)
    {

        personList = daoPerson.GetPersonsExt(currentPage, pageSize, ref totals, "%");
        dgrPersonel.DataSource = personList;
        dgrPersonel.DataBind();

    }
    protected void dgrPersonel_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //dgrPersonel.EditIndex = e.NewEditIndex;
        //LoadData(pageSize, currentPage, ref totals, txtSearch.Text);

        Person person = daoPerson.GetPerson(int.Parse(dgrPersonel.DataKeys[e.NewEditIndex].Value.ToString()));
        txtFirstName.Text = person.FirstName;
        txtLastName.Text = person.LastName;
        txtEmail.Text = person.Email;
        txtAddress.Text = person.Address;
        pnlEditBox.Visible = true;
        if (person.Gender) radMale.Checked = true; else radFemale.Checked = true;
        dgrPersonel.EditIndex = e.NewEditIndex;
        LoadData(pageSize, currentPage, ref totals, "%");
    }
    protected void dgrPersonel_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgrPersonel.EditIndex = -1;
        LoadData(pageSize, currentPage, ref totals, "%");
    }
    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        pnlEditBox.Visible = false;
        dgrPersonel.EditIndex = -1;
        LoadData(pageSize, currentPage, ref totals, "%");
    }
    protected void lnkSearch_Click(object sender, EventArgs e)
    {
        personList = daoPerson.GetPersonsExt(currentPage, pageSize, ref totals, "%" + txtSearch.Text + "%");
        dgrPersonel.DataSource = personList;
        dgrPersonel.DataBind();
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int count = 0;
        bool rs = true;
        foreach (GridViewRow row in dgrPersonel.Rows)
        {
            bool check = ((CheckBox)row.FindControl("chkChoice")).Checked;
            if (check)
            {
                count++;
                int id=int.Parse(dgrPersonel.DataKeys[row.RowIndex].Value.ToString());
                if (daoPerson.Delete(id) <= 0)
                {
                    rs = false;
                    ShowMessage("Error has occured. Some selected items cannot be deleted");
                    break;
                }
            }
            
        }
        if (count > 0)
        {

            if (rs) ShowMessage("All seleted items are deleted successfully");
            else ShowMessage("Error has occured. Some item may not be deleted.");
        }
        else ShowMessage("Please select a item!");
        LoadData(pageSize, currentPage, ref totals, "%");
    }
   
}
